package com.yan.activity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.yan.R;
import com.yan.network.HttpService;

public class DataManager {
	

	String url = "http://www.diyphonegadgets.com/feeds/posts/default?alt=rss";
	
	
	private static final String TAG = "DATAMANAGER";

	private static DataManager sInstance;
	
	public static DataManager getInstance()
	{
		if(sInstance == null)
		{
			sInstance = new DataManager();
		}
		return sInstance;
	}
	
	private HashMap<String, Bitmap> mtImages;
	
	private HashMap<String,  List<Integer>> cacheForGallery;

	
	private DataManager()
	{
		mtImages = new HashMap<String, Bitmap>();
		cacheForGallery=new HashMap<String,  List<Integer>>();
	}



	public List<Article> getArticles()
	{
		List<Article> articles = null;
		try
		{
			articles = HttpService.getArticles();
		}
		catch(Exception e)
		{
			Debug.debugPermanent(TAG, "Exception : getLatestArticles -> "+e.getMessage());
			e.printStackTrace();
			return null;
		}
		return articles;
	}
	
	


/*	public Article getArticleDetails(String articleId)
	{
		Article film = null;
		try
		{
			film = HttpService.getArticleDetails(articleId);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return film;
	}
	
	*/
	
	
	/*************************
	 * 
	 * 		  UTILS
	 * 
	 ************************/
	
	public void setBitmapToImageView(String url, ImageView imageView, String id)
	{
		imageView.setImageResource(R.drawable.diy);

		if(mtImages.containsKey(id))
		{
			Bitmap bmp = mtImages.get(id);
			if(bmp != null){
				imageView.setImageBitmap(bmp);
			}
			
		}
		else
		{
			new ImageViewUpdater(imageView, id).execute(url);
		}
	}
	
	

	public void setImageListToGallery(String link, BetterGallery gallery, Article article, Context c)
	{
		
		gallery.setAdapter(new ImageAdapter(c, new ArrayList<Integer>()));
		
		if(cacheForGallery.containsKey(link))
		{
			List<Integer> imgList = cacheForGallery.get(link);
			if(imgList != null){
				gallery.setAdapter(new ImageAdapter(c, imgList));
				gallery.setSpacing(5);
				gallery.setSelection(gallery.getCount()-1,true);
			}
			
		}
		else
		{
			new GalleryUpdater(gallery, article, c).execute(article);
		}
	}

	
	
	public void addBitmap(String imgID, Bitmap bmp)
	{
		mtImages.put(imgID, bmp);
	}
	
	
	public void addImageList(Article mCurrentarticle,  List<Integer> imageList){
		
		
        cacheForGallery.put(mCurrentarticle.getLink(), imageList);

	}
	
	public String getFormattedLength(String length)
	{
		int totalMins = Integer.valueOf(length.substring(0, length.indexOf("min")));
		int hours = totalMins / 60;
		int mins = totalMins - (60 * hours);

		return hours + "hr " + mins + "mins";
	}
	

	
    

	

}
